package com.itheima.controller;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.domain.Employee;
import com.itheima.domain.R;
import com.itheima.service.EmployeeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

@RestController
@RequestMapping("/employee")
public class EmployeeController {

    @Autowired
    private EmployeeService employeeService;

    @Autowired
    private HttpServletRequest httpServletRequest;

    //账户登录功能
    @PostMapping("/login")
    public R<Employee> login(@RequestBody Employee employee){
        // ①. 将页面提交的密码password进行md5加密处理, 得到加密后的字符串
        String password = employee.getPassword();
        password = DigestUtils.md5DigestAsHex(password.getBytes());
        //②. 根据页面提交的用户名username查询数据库中员工数据信息
        LambdaQueryWrapper<Employee> wapper = new LambdaQueryWrapper<>();
        wapper.eq(Employee::getUsername,employee.getUsername());
        Employee one = employeeService.getOne(wapper);
        //③. 如果没有查询到, 则返回登录失败结果
        if(one==null){
            return R.error("没有此用户");
        }
        //④. 密码比对，如果不一致, 则返回登录失败结果
        if(!one.getPassword().equals(password)){
            return R.error("密码错误");
        }
        //⑤. 查看员工状态，如果为已禁用状态，则返回员工已禁用结果
        if(one.getStatus()==0){
            return R.error("此用户已被禁用");
        }
        //⑥. 登录成功，将员工id存入Session, 并返回登录成功结果
        httpServletRequest.getSession().setAttribute("employee",one.getId());

        return R.success(one);
    }

    //账户退出功能
    @PostMapping("/logout")
    public R logOut(){
        //清空session
        HttpSession session = httpServletRequest.getSession();
        session.setAttribute("employee",null);

        return R.success("退出成功");
    }

    //添加用户功能
    @PostMapping
    public R save(@RequestBody Employee employee){
        //判断用户是否存在
        LambdaQueryWrapper<Employee> wapper = new LambdaQueryWrapper<>();
        wapper.eq(Employee::getUsername,employee.getUsername());
        Employee user = employeeService.getOne(wapper);

        if(user != null){
            return R.error("添加失败，该用户以存在");
        }
        //补全数据
        /*HttpSession session = httpServletRequest.getSession();
        Long userId = (Long) session.getAttribute("employee");

        employee.setCreateTime(LocalDateTime.now());
        employee.setUpdateTime(LocalDateTime.now());
        employee.setCreateUser(userId);
        employee.setUpdateUser(userId);*/

        //设置初始密码
        String password = DigestUtils.md5DigestAsHex("123456".getBytes());
        employee.setPassword(password);

        //更新到数据库
        employeeService.save(employee);
        return R.success("添加成功");
    }

    //分页条件查询功能
    @GetMapping("/page")
    public R select(Integer page,Integer pageSize,String name){
        IPage<Employee> pageInfo = new Page<>(page,pageSize);
        LambdaQueryWrapper<Employee> wapper = new LambdaQueryWrapper<>();
        if(name!=null){
            //根据名字进行查询
            wapper.like(Employee::getName,name);
        }
        IPage<Employee> iPage = employeeService.page(pageInfo, wapper);
        return R.success(iPage);
    }

    //修改启用，禁用状态功能
    @PutMapping
    public R update(@RequestBody Employee employee){
        //根据id查询修改
        Employee byId = employeeService.getById(employee.getId());
        if(byId==null){
            return R.error("没有该用户");
        }
        if(employee.getId()==1){
            return R.error("此用户不能被禁用");
        }
        Long empId = (Long) httpServletRequest.getSession().getAttribute("employee");
        //更新状态
        //byId.setStatus(employee.getStatus());
        //更新时间
        //employee.setUpdateTime(LocalDateTime.now());
        //更新修改人员
        //employee.setUpdateUser(empId);
        //更改数据库
        //employeeService.updateById(byId);

        if(empId.longValue() == employee.getId() || empId.longValue() ==1){
            employeeService.updateById(employee);
            return R.success("状态修改成功");
        }else {
            return R.error("您的权限不足");
        }
    }

    //修改数据回显功能
    @GetMapping("/{id}")
    public R findById(@PathVariable Long id){
        if(id==null){
            return R.error("查询失败");
        }
        Employee serviceById = employeeService.getById(id);
        return R.success(serviceById);
    }
}
